/**
 * 初始化地图 卫星图
 * @param {*} ele 标签id
 * @param {*} center 中心点坐标
 * @param {*} zoom 缩放级别
 * @returns map 地图对象
 */
export function initMap(ele, center, zoom = 12) {
  const map = new AMap.Map("container", {
    viewMode: '3D',
    zoom: zoom, //地图级别
    center: center
  });
  //异步加载控件
  AMap.plugin('AMap.Geolocation', function () {
    var geolocation = new AMap.Geolocation({
      position: 'LB', // 定位按钮的位置
      offset: [10, 110], // 定位按钮的偏移量
    });
    map.addControl(geolocation); //添加控件
  });
  return map
}

/**
 * 地图添加多边形覆盖物
 * @param {*} map       地图
 * @param {*} pointList 网格坐标点的集合
 * @returns 多边形覆盖物
 */
export function addPolygonOverLay(map, pathArr, color, zoom) {
  if (zoom != 5) {
    map.setCenter(pathArr[0][0]);
  }
  map.setZoom(zoom)
  let polygon = new AMap.Polygon({
    path: pathArr, //多边形路径
    fillColor: color, //多边形填充颜色
    strokeOpacity: 1, //线条透明度
    fillOpacity: 0.2, //填充透明度
    strokeColor: color, //线条颜色
    strokeWeight: 1, //线条宽度
    strokeStyle: "solid", //线样式
    strokeDasharray: [5, 5], //轮廓的虚线和间隙的样式
  });
  // 向地图上添加面
  map.add(polygon)
  return polygon
}


/**
 * 测距
 * @param {*} map 地图
 * @param {*} zoom 
 */
export function lineTool(map, zoom = 12) {
  let config = {
    showLabel: true,
    color: "#4AFFEE",
    lineStyle: 'dashed'
  };
  let lineTool = new T.PolylineTool(map, config);
  return lineTool
  // lineTool.open();
}


/**
 * 自定义图片标注
 * @param {*} map 地图
 * @param {*} zoom 
 */
export function addCustomMark(map, img, lnglat, wh, zoom = 12) {
  //创建图片对象
  var icon = new T.Icon({
    iconUrl: img,
    iconSize: new T.Point(wh.w, wh.h),
    iconAnchor: new T.Point(10, 25)
  });
  //向地图上添加自定义标注
  var marker = new T.Marker(new T.LngLat(lnglat.lng, lnglat.lat), { icon: icon });
  map.addOverLay(marker);
  return marker;
}

/**
 * 添加文本标注
 * @param {*} map
 * @param {*} point
 */
export function addLabelMaker({
  point,
  name,
}) {
  // 创建文本标注对象
  var text = new AMap.Text({
    text: name,
    position: point,
    anchor: 'center', // 设置文本标记锚点
    draggable: true,
    cursor: 'pointer',
    style: {
      'padding': '.75rem 1.25rem',
      'margin-bottom': '1rem',
      'border-radius': '.25rem',
      'background-color': 'white',
      'width': '15rem',
      'border-width': 0,
      'box-shadow': '0 2px 6px 0 rgba(114, 124, 245, .5)',
      'text-align': 'center',
      'font-size': '20px',
      'color': 'blue'
    },
  });
  // 将文本标注添加到地图
  // text.setMap(map);
  return text;
}


/**
 * 添加文本标注起点
 * @param {*} map
 * @param {*} point
 */
export function addLabelStartMaker({
  map,
  point,
  text,
  offset = { x: -80, y: -40 }
}) {
  const label = new T.Label({
    text: text == '起点' ? `<div class='images img1'>${text}</div>` : `<div class='images img2'>${text}</div>`,
    position: new T.LngLat(point.lng, point.lat),
    offset: new T.Point(offset.x, offset.y)
  })
  map.addOverLay(label)
  return label
}
/**
 * 以某坐标、半径为中心 绘制圆
 * @param {*} map
 */
export function addCircleLayer({
  map,
  point,
  radius = 2000,
  weight = 2,
  color = '#52CDD7',
  opacity = 1,
  fillColor = '#52CDD7',
  fillOpacity = 0.4
}) {
  // 定义该矩形的显示区域
  var circle = new T.Circle(new T.LngLat(point.lng, point.lat), radius, {
    color,
    weight,
    opacity,
    fillColor,
    fillOpacity
  })
  // 向地图上添加圆
  map.addOverLay(circle)
  return circle
}

/**
 * 地图添加多边形覆盖物
 * @param {*} map       地图
 * @param {*} pointList 网格坐标点的集合
 * @returns 多边形覆盖物
 */
export function addPolygonOverLay1({
  map,
  pointList,
  color,
  fillColor,
  showCenter = true,
  zoom = 12,
  weight = 2,
  opacity = 1,
  fillOpacity = 0,
  lineStyle = 'dashed'
}) {
  if (color == '' || color == undefined) {
    color = '#FF0000'
    fillColor = "#FF0000"
  }
  if (showCenter) {
    const center = calculateCenter(pointList)
    map.centerAndZoom(new T.LngLat(center.lng, center.lat), zoom)
  }
  var points = []
  pointList.forEach(e => {
    points.push(new T.LngLat(e.lng, e.lat))
  })
  // 创建面对象
  var polygon = new T.Polygon(
    points, {
    color,
    weight,
    opacity,
    fillColor,
    fillOpacity,
    lineStyle
  }
  )
  // 向地图上添加面
  map.addOverLay(polygon)
  return polygon
}

/**
 * 添加文本标注
 * @param {*} map
 * @param {*} point
 */
export function addCustLabelMaker({
  map,
  point,
  text,
  offset = { x: -75, y: -10 }
}) {
  // let icon = require("@/assets/dws.png")
  const label = new T.Label({
    text: `<div style=" background: #fff;
    padding: 4px 10px;
    color: #000;
    border-radius: 5px;
    display: flex;
    align-items: center;"><img src="${icon}" style='margin-right:10px' />${text}</div>`,
    position: new T.LngLat(point.lng, point.lat),
    offset: new T.Point(offset.x, offset.y)
  })
  map.addOverLay(label)
  return label
}

/**
 * 以某坐标、半径为中心 绘制圆
 * @param {*} map
 */
export function addCircleLayer1({
  map,
  point,
  showCenter = true,
  radius = 50,
  weight = 2,
  color = '#FF0000',
  opacity = 1,
  fillColor = '#FF0000',
  fillOpacity = 0,
  lineStyle = 'dashed'
}) {
  // 定义该矩形的显示区域
  var circle = new T.Circle(new T.LngLat(point.longitude, point.latitude), radius, {
    color,
    weight,
    opacity,
    fillColor,
    fillOpacity,
    lineStyle
  })
  // 向地图上添加圆
  map.addOverLay(circle)
  return circle
}
/**
 * 计算某区域的中心点坐标
 * @param {*} points 
 * @returns 
 */
export function calculateCenter(points) {
  console.log(points)
  var center = null
  if (points.length === 0) return center
  var lngList = []
  var latList = []
  points.forEach(item => {
    lngList.push(item.lng)
    latList.push(item.lat)
  })
  const lngSum = lngList.reduce((s1, s2) => s1 + s2)
  console.log(lngSum)
  const lngAverage = (lngSum / lngList.length).toFixed(5)
  const latSum = latList.reduce((s1, s2) => s1 + s2)
  const latAverage = (latSum / latList.length).toFixed(5)
  center = {
    lng: Number(lngAverage),
    lat: Number(latAverage)
  }
  return center
}

/**
 * 绘制矩形
 * @param {*} map
 */
export function addRectLayer({
  map,
  point,
  point1,
  color = '#52CDD7',
  opacity = 1,
  fillColor = '#52CDD7',
  fillOpacity = 0.4
}) {
  var bounds = new T.LngLatBounds(new T.LngLat(point.lng, point.lat), new T.LngLat(point1.lng, point1.lat));
  var rect = new T.Rectangle(bounds, {
    color,
    opacity,
    fillColor,
    fillOpacity
  });
  map.addOverLay(rect);
  return rect
}

/**
 * 绘制线
 * @param {*} map
 */
export function drawPolyline({
  map,
  point,
  weight,
  color,
  lineStyle
}) {
  if (!color) {
    color = '#52CDD7'
  }
  if (!weight) {
    weight = 3
  }
  //创建线对象
  var line = new T.Polyline(point, { color, weight, lineStyle });
  //向地图上添加线
  map.addOverLay(line);
  return line
}


/**
 * 
 * @param {*} el  标签id
 * @param {*} flag 是否全屏
 */
export function isFullScreen(el, fullscreen) {
  let element = document.getElementById(el);
  if (!fullscreen) {
    // 全屏
    if (element.requestFullscreen) {
      element.requestFullscreen();
    } else if (element.webkitRequestFullScreen) {
      element.webkitRequestFullScreen();
    } else if (element.mozRequestFullScreen) {
      element.mozRequestFullScreen();
    } else if (element.msRequestFullscreen) {
      // IE11
      element.msRequestFullscreen();
    }
  }
  fullscreen = !fullscreen;
}
/**退出全屏 */
export function exitFullScreen() {
  if (document.exitFullscreen) {
    document.exitFullscreen();
  } else if (document.mozCancelFullScreen) { /* Firefox */
    document.mozCancelFullScreen();
  } else if (document.webkitExitFullscreen) { /* Chrome, Safari and Opera */
    document.webkitExitFullscreen();
  } else if (document.msExitFullscreen) { /* IE/Edge */
    document.msExitFullscreen();
  }

}